package com.bo.gather.rule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bo.gather.bean.ContentBean;
import com.bo.gather.bean.GatherBean;
import com.bo.gather.bean.Lengthinfo;
import com.bo.gather.bean.MustIncludeInfo;
import com.bo.gather.bean.RemoveInfo;
import com.bo.gather.bean.ReplaceInfo;



public class GatherRule  extends BaseRule{

	

	
	/**
	 * 计算子节点内容
	 * @param parentstr
	 * @param gatherbean
	 */
	public void calculateChildNote(String childstr,GatherBean gatherbean){
		//处理子节点信息
		String []child=childstr.split("\r\n");
		Map<String,String> childmap=new HashMap<String,String>();
		List<String> childlist=new ArrayList<String>();
		for(String childrule:child){
			if(!childrule.trim().equals("")){
				int dengyu=childrule.indexOf("=");
				String key=childrule.substring(0, dengyu);
				String value=childrule.substring(dengyu+1, childrule.length());
				childmap.put(key, value);
				if(key.indexOf("gather")==-1 && key.indexOf("rule")==-1){
					childlist.add(key);
				}
			}
			
		}
		gatherbean.setChildUrl(childmap.get(GatherRule.gatherchildurl));
		gatherbean.setChildHtml(this.getContentBean(childmap.get(GatherRule.gatherchildhtml), childmap.get(GatherRule.gatherchildhtmlrule)));
		if(childlist.size()>0){
			gatherbean.setChildContent(new ArrayList<ContentBean>());
			for(String s:childlist){
				ContentBean cb=this.getContentBean(childmap.get(s), childmap.get(s+"_rule"));
				cb.setName(s);
				gatherbean.getChildContent().add(cb);
			}
		}
	}
	
	/**
	 * 计算父节点内容
	 * @param parentstr
	 * @param gatherbean
	 */
	public void calculateParentNote(String parentstr,GatherBean gatherbean){
		//处理父节点信息
		String []parent=parentstr.split("\r\n");
		Map<String,String> parentmap=new HashMap<String,String>();
		List<String> list=new ArrayList<String>();
		for(String parentrule:parent){
			if(!parentrule.trim().equals("")){
				int dengyu=parentrule.indexOf("=");
				String key=parentrule.substring(0, dengyu);
				String value=parentrule.substring(dengyu+1, parentrule.length());
//				System.out.println(key+"====="+value);
				parentmap.put(key, value);
				if(key.indexOf("gather")==-1 && key.indexOf("rule")==-1){
					list.add(key);
				}
			}
		}
		
		gatherbean.setTitle(parentmap.get(GatherRule.gathertitle));
		gatherbean.setCode(parentmap.get(GatherRule.gathercode));
		gatherbean.setParentUrl(parentmap.get(GatherRule.gatherparenturl).split(";"));
		gatherbean.setOnlyid(parentmap.get(GatherRule.gatherparentonlyid));
		gatherbean.setParentHtml(this.getContentBean(parentmap.get(GatherRule.gatherparenthtml), parentmap.get(GatherRule.gatherparenthtmlrule)));
		if(list.size()>0){
			gatherbean.setParentContent(new ArrayList<ContentBean>());
			for(String s:list){
				ContentBean cb=this.getContentBean(parentmap.get(s), parentmap.get(s+"_rule"));
				cb.setName(s);
				gatherbean.getParentContent().add(cb);
			}
		}
	}
	
	
	public ContentBean getContentBean(String content,String rule){
		ContentBean contentbean=new ContentBean();
//		contentbean.setContent(content);
		String []contentarray=content.split("◎");
		if(contentarray!=null && contentarray.length==2){
			contentbean.setContentStart(contentarray[0]);
			contentbean.setContentEnd(contentarray[1]);
		}
		
		if(rule!=null){
			String []rulearray=rule.replaceAll("≤", "").split("≥");
			for(String s:rulearray){
				//替换
				ReplaceInfo replaceInfo=this.calculateReplace(s);
				if(replaceInfo!=null){
					if(contentbean.getReplaceList()==null){
						List<ReplaceInfo> replacelist=new ArrayList<ReplaceInfo>();
						replacelist.add(replaceInfo);
						contentbean.setReplaceList(replacelist);
					}else{
						contentbean.getReplaceList().add(replaceInfo);
					}
					continue;
				}
				//删除操作
				RemoveInfo removeInfo=this.calculateRemove(s);
				if(removeInfo!=null){
					if(contentbean.getRemoveList()==null){
						List<RemoveInfo> removelist=new ArrayList<RemoveInfo>();
						removelist.add(removeInfo);
						contentbean.setRemoveList(removelist);
					}else{
						contentbean.getRemoveList().add(removeInfo);
					}
					continue;
				}
				//必须包含操作
				MustIncludeInfo mustIncludeInfo=this.calculateMustInclude(s);
				if(mustIncludeInfo!=null){
					if(contentbean.getMustList()==null){
						List<MustIncludeInfo> mustlist=new ArrayList<MustIncludeInfo>();
						mustlist.add(mustIncludeInfo);
						contentbean.setMustList(mustlist);
					}else{
						contentbean.getMustList().add(mustIncludeInfo);
					}
					continue;
				}
				
				
				//计算长度
				Lengthinfo lengthinfo=this.calculateLength(s);
				if(lengthinfo!=null){
					contentbean.setLengthinfo(lengthinfo);
				}
				
			}
		}
		
		return contentbean;
	}
	
	

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		String sb=ReadRule.readFile(new File("D:\\bo\\优酷.txt"));
//		System.out.println(sb);
//		
//		ReadRule rr=new ReadRule();
//		rr.ReadRule(new File("D:\\bo\\优酷.txt"));
		
		System.out.println("".indexOf(""));
	}

}
